#include <QStackedWidget>
#include <QLabel>
#include <QRoundProgressBar.h>
#include <QUrl>
#include <QVBoxLayout>
#include <QThread>
#include <QFileInfo>
#include <QMovie>
#include <QDebug>
#include <QScrollArea>
#include <QHBoxLayout>

#include "imageitem.h"
#include "filedownloader.h"

class ImageItemPrivate
{
public:
    ImageItemPrivate(ImageItem* q);
    bool isMovie(const QUrl& url);

    QUrl url;
    QString description;
    QString localPath;

    ImageItem* q_ptr;
    QScrollArea* scrollArea;
    QStackedWidget* stackWidget;
    QLabel* imageLabel;
    QLabel* descLabel;
    QRoundProgressBar* pgsBar;
};

ImageItemPrivate::ImageItemPrivate(ImageItem *q) : q_ptr(q)
{
    QVBoxLayout* layout = new QVBoxLayout;
    layout->setContentsMargins(0, 0, 0, 0);
    scrollArea = new QScrollArea(q_ptr);
    scrollArea->setWidgetResizable(true);
    stackWidget = new QStackedWidget(q_ptr);
    imageLabel = new QLabel(q_ptr);
    descLabel = new QLabel(q_ptr);
    descLabel->setAlignment(Qt::AlignTop | Qt::AlignLeft);

    QWidget* labelWidget = new QWidget(q_ptr);
    QHBoxLayout* labelWidgetLayout = new QHBoxLayout;
    labelWidgetLayout->addWidget(imageLabel);
    labelWidgetLayout->addWidget(descLabel);
    labelWidget->setLayout(labelWidgetLayout);

    scrollArea->setWidget(labelWidget);

    pgsBar = new QRoundProgressBar(q_ptr);
    pgsBar->setBarStyle(QRoundProgressBar::StyleLine);
    pgsBar->setValue(0);

    stackWidget->addWidget(pgsBar);
    stackWidget->addWidget(scrollArea);
    layout->addWidget(stackWidget);
    q_ptr->setLayout(layout);

    q_ptr->setMinimumHeight(200);

}

bool ImageItemPrivate::isMovie(const QUrl &url)
{
    QFileInfo fi(url.toString());
    return fi.suffix().compare("gif", Qt::CaseInsensitive) == 0;
}

ImageItem::ImageItem(QWidget *parent) : QWidget(parent)
{
    d_ptr = new ImageItemPrivate(this);
}

ImageItem::~ImageItem()
{
    delete d_ptr;
}

void ImageItem::setUrl(const QUrl& url)
{
    if (d_ptr->url != url) {
        d_ptr->url = url;

        // show pgsbar and download image
        d_ptr->stackWidget->setCurrentWidget(d_ptr->pgsBar);
        d_ptr->pgsBar->setValue(0);

        QThread* thread = new QThread();
        Jcr::FileDownloader* fd = new Jcr::FileDownloader(url);
        fd->setSaveDir("G:/HAHACRAWLERDATA");

        connect(thread, SIGNAL(started()), fd, SLOT(start()));
        connect(fd, SIGNAL(finished()), thread, SLOT(terminate()));
        connect(fd, SIGNAL(finished()), thread, SLOT(deleteLater()));
        connect(fd, SIGNAL(percentCompleted(float)), this, SLOT(onPercentCompleted(float)));

        thread->start();
    }
}

QUrl ImageItem::url() const
{
    return d_ptr->url;
}

void ImageItem::setDescription(const QString& desc)
{
    d_ptr->descLabel->setText(desc);
}

QString ImageItem::description() const
{
    return d_ptr->descLabel->text();
}

QString ImageItem::localPath() const
{
    return d_ptr->localPath;
}

void ImageItem::onPercentCompleted(float percent)
{
    if (percent == 1.0) {

        Jcr::FileDownloader* fd = qobject_cast<Jcr::FileDownloader*>(sender());

        d_ptr->stackWidget->setCurrentWidget(d_ptr->scrollArea);
        d_ptr->localPath = fd->localPath();

        if (d_ptr->isMovie(d_ptr->localPath)) {
            QMovie* mv = new QMovie(this);
            mv->setFileName(d_ptr->localPath);
            d_ptr->imageLabel->setMovie(mv);
            mv->start();
        } else {
            d_ptr->imageLabel->setPixmap(QPixmap(d_ptr->localPath));
        }

        fd->deleteLater();
    } else {
        d_ptr->pgsBar->setValue(percent * 100);
    }
}



